package com.ita.acmsystem.merchant.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.acmsystem.merchant.entity.Merchant;
import com.ita.acmsystem.merchant.service.IMerchantService;
import com.ita.acmsystem.merchant.service.impl.MerchantServiceImpl;
import com.ita.acmsystem.merchant.util.SimpleJsonUtil;

/**
 * Servlet implementation class AjaxQueryMerchantServlet
 */
public class AjaxQueryMerchantServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	private IMerchantService service = new MerchantServiceImpl();
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AjaxQueryMerchantServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		queryMerchant(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		queryMerchant(request, response);
	}

	private void queryMerchant(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		HttpSession session = request.getSession();
		String id = ((Merchant)session.getAttribute("user")).getMerchantId();
		
		Merchant merchant = service.getMerchantById(id);
		String json = SimpleJsonUtil.format(merchant);
		PrintWriter writer = response.getWriter();
		writer.write(json);
		writer.close();
	}
}
